/**
 * Created by fengxiaojuan on 2018/8/28.
 */
'use strict';
cBoard.service('chartPie3DService', function ($state, $window) {

    //CBoardHighChartsRender
    this.render = function (containerDom, option, scope, persist, drill, relations, chartConfig) {
        console.log(option)
        var render = new CBoardHighChartRender(containerDom, option);
        //render.addClick(chartConfig, relations, $state, $window);
        return render.chart(null, persist);
    };

    this.parseOption = function (data) {
        console.log(data);
        var chartConfig = data.chartConfig;
        var casted_keys = data.keys;
        var casted_values = data.series;
        var aggregate_data = data.data;
        var newValuesConfig = data.seriesConfig;

        var series = new Array();
        var string_keys = _.map(casted_keys, function (key) {
            return key.join('-');
        });
        var string_value = _.map(casted_values, function (value) {
            return value.join('-');
        });
        var b = 100 / (casted_values.length * 9 + 1);
        var titles = [];
        for (var i = 0; i < aggregate_data.length; i++) {
            var joined_values = casted_values[i].join('-');
            var realType = angular.copy(newValuesConfig[joined_values]).type;
            var s = {
                name: string_value[i],
                type: 'pie',
                data: []
            };

            titles.push({
                textAlign: 'center', textStyle: {
                    fontSize: 12,
                    fontWeight: 'normal'
                }, text: string_value[i], left: 5 * b + i * 9 * b + '%', top: '90%'
            });
            for (var j = 0; j < aggregate_data[i].length; j++) {
                s.data.push([
                    string_keys[j],
                    _.isUndefined(Number(aggregate_data[i][j])) ? 0 : Number(aggregate_data[i][j])
            ]);
            }
            series.push(s);
        }
        var echartOption = {
            chart: {
                backgroundColor: 'rgba(0,0,0,0)',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie',
                options3d: {
                    enabled: true,
                    alpha: 45,
                    beta: 0
                }
            },
            title: titles,
            legend: {
                orient: 'vertical',
                x: 'left',
                data: string_keys
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    depth: 35,
                    dataLabels: {
                        enabled: true,
                        format: '{point.name}'
                    }
                }
            },
            series: series
        };
        if (realType == 'doughnut3D') {
            echartOption.plotOptions.pie.innerSize = 150;
            echartOption.plotOptions.pie.depth = 45;
        }

        updateEchartOptions(chartConfig.option, echartOption);

        return echartOption;
    };
});